using UnityEngine;
using System.Collections;

public class Buttons : MonoBehaviour {
    
    public static TreeNode root;
    public static TreeNode[] list;
    public Texture2D black; // Tbe color for the splits
    public Texture2D grey; // The dungeon color
    public Texture2D green; //The color of the fill to the rooms
    public Texture2D red; // The color of the corridors
    public string userRatio; 
    public string userNumberOfSplits;
    public string userMaxRoom;
    public string userMinRoom;
    public string userStartX;
    public string userStartY;
    public string userWidth;
    public string userHeight;
    public int startX;
    public int startY;
    public int dungeonWidth;
    public int dungeonHeight;
    public static float maxRoomSize;
    public static int minRoomSize;
    public int numberOfSplits;
    private int splitNum = 0;
    private bool done = false;

    // Use this for initialization
    void Start () {
        userStartX = "0";
           userStartY = "0";
           userWidth = "500";
        userHeight = "500";
        userRatio = "2";
        userNumberOfSplits = "0";
        userMinRoom = "10";
        userMaxRoom = "100";
        startX = 250 + int.Parse(userStartX);
        startY = 100 + int.Parse(userStartY);
        dungeonWidth =  0 + int.Parse(userWidth);
        dungeonHeight =  0 + int.Parse(userHeight);
        TreeNode.ratio = int.Parse(userRatio);
        minRoomSize = int.Parse(userMinRoom);
        maxRoomSize = int.Parse(userMaxRoom);
        
        
        root = new TreeNode(startX,startY,dungeonWidth,dungeonHeight);
        list = new TreeNode[3];
        list[0] = root;
        
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    
    void OnGUI()
    {
        GUI.Label(new Rect(300, 10, 300, 50), "Based off  http://roguebasin.roguelikedevelopment.org/index.php/Basic_BSP_Dungeon_generation algorithm");
        
        if(Event.current.type.Equals(EventType.Repaint))
            {
                Graphics.DrawTexture(new Rect(startX,startY, dungeonWidth, dungeonHeight), grey);
            }
        
        if(GUI.Button(new Rect(10,10,150,50), "Split"))
        {
            //Call the split function so the the random is done for the horizontal
            list[0].split();                 
        }                
        if(GUI.Button(new Rect(10,70,150,50), "Generate All Rooms"))
        {
            list[0].generateRooms();
            done = true;
        }
        
        if(GUI.Button(new Rect(10,130,150,50), "Generate Corridors"))
        {
            if(done)
            {
                list[0].generateCorridors(TreeNode.counter);
            }
            else
            {
                list[0].generateRooms();
                done = true;
                list[0].generateCorridors(TreeNode.counter);
            }
        }
        
        if(GUI.Button(new Rect(10,190,150,50), "Generate Rooms and Corridors"))
        {
            list[0].generateRooms();
            done = true;
            list[0].generateCorridors(TreeNode.counter);
        }
        
        
        
        GUI.Label(new Rect(10, 240, 150, 150), "Number of splits:");
        userNumberOfSplits = GUI.TextField(new Rect(110, 244, 50, 20), userNumberOfSplits, 25);
        if(GUI.Button(new Rect(170, 244, 50, 20), "GO"))
        {
            splitNum = int.Parse(userNumberOfSplits);
            startX = 250 + int.Parse(userStartX);
            startY = 100 + int.Parse(userStartY);
            dungeonWidth =  0 + int.Parse(userWidth);
            dungeonHeight =  0 + int.Parse(userHeight);
            TreeNode.ratio = int.Parse(userRatio);
            done = false;
            minRoomSize = int.Parse(userMinRoom);
               maxRoomSize = int.Parse(userMaxRoom);
            
            if(dungeonWidth == 0)
            {
                dungeonWidth = 500;
            }
            if(dungeonHeight == 0)
            {
                dungeonHeight = 500;
            }
            if(TreeNode.ratio == 0)
            {
                TreeNode.ratio = 2;
            }
            if(minRoomSize > maxRoomSize)
            {
                minRoomSize = (int)maxRoomSize;
            }
            if(maxRoomSize < 50)
            {
                maxRoomSize = 50;
            }
            if(minRoomSize <= 0)
            {
                minRoomSize = 10;
            }
            if(maxRoomSize > 100)
            {
                maxRoomSize = 100;
            }
            
            root = new TreeNode(startX,startY,dungeonWidth,dungeonHeight);
            list = new TreeNode[3];
            list[0] = root;
            TreeNode.counter = 1;
            
            if(splitNum > 0)
            {
                for(int i = 0; i < splitNum; i++)
                {
                    list[0].split();
                }
                list[0].generateRooms();
                list[0].generateCorridors(TreeNode.counter);
            }
        }
        GUI.Label(new Rect(10, 262, 150, 150), "Max room Size:(%) ");
        userMaxRoom = GUI.TextField(new Rect(123, 266, 50, 20), userMaxRoom, 25);
        GUI.Label(new Rect(10, 284, 150, 150), "Min room Size:");
        userMinRoom = GUI.TextField(new Rect(110, 288, 50, 20), userMinRoom, 25);
        GUI.Label(new Rect(10, 320, 150, 150), "Dungeon start x: ");
        userStartX = GUI.TextField(new Rect(109, 320, 50, 20), userStartX, 25);
        GUI.Label(new Rect(10, 345, 150, 150), "Dungeon start y: ");
        userStartY = GUI.TextField(new Rect(109, 345, 50, 20), userStartY, 25);
        GUI.Label(new Rect(10, 370, 150, 150), "Dungeon Width: ");
        userWidth = GUI.TextField(new Rect(109, 370, 50, 20), userWidth, 25);
        GUI.Label(new Rect(10, 395, 150, 150), "Dungeon Height: ");
        userHeight = GUI.TextField(new Rect(109, 395, 50, 20), userHeight, 25);
        GUI.Label(new Rect(10, 420, 150, 150), "Ratio: ");
        userRatio = GUI.TextField(new Rect(109, 420, 50, 20), userRatio, 25);
       
        if(GUI.Button(new Rect(10,445,150,50), "Reset Dungeon"))
        {
            startX = 250 + int.Parse(userStartX);
            startY = 100 + int.Parse(userStartY);
            dungeonWidth =  0 + int.Parse(userWidth);
            dungeonHeight =  0 + int.Parse(userHeight);
            TreeNode.ratio = int.Parse(userRatio);
            done = false;
            minRoomSize = int.Parse(userMinRoom);
               maxRoomSize = int.Parse(userMaxRoom);
            
            if(dungeonWidth == 0)
            {
                dungeonWidth = 500;
            }
            if(dungeonHeight == 0)
            {
                dungeonHeight = 500;
            }
            if(TreeNode.ratio == 0)
            {
                TreeNode.ratio = 2;
            }
            if(minRoomSize > maxRoomSize)
            {
                minRoomSize = (int)maxRoomSize;
            }
            if(maxRoomSize < 50)
            {
                maxRoomSize = 50;
            }
            if(minRoomSize <= 0)
            {
                minRoomSize = 10;
            }
            if(maxRoomSize > 100)
            {
                maxRoomSize = 100;
            }
            
            root = new TreeNode(startX,startY,dungeonWidth,dungeonHeight);
            list = new TreeNode[3];
            list[0] = root;
            TreeNode.counter = 1;
            
            if(splitNum > 0)
            {
                for(int i = 0; i < splitNum; i++)
                {
                    list[0].split();
                }
                list[0].generateRooms();
                list[0].generateCorridors(TreeNode.counter);
            }
        }
        
        if(Event.current.type.Equals(EventType.Repaint))
        {
            for(int i = 0; i < TreeNode.counter; i++)
            {
                if(list[i].horizontal)
                {
                    //Draw a horizontal line starting from the random X and Y coordinate inside of the
                    //parent to the width or height of the room.The split line has a thickness of 5 pixels
                    //for readability.
                    Graphics.DrawTexture(new Rect(list[i].rightChild.x,
                                                  list[i].rightChild.y,
                                                  list[i].width,2),black);
                }
                else if(list[i].rightChild != null)
                {
                    Graphics.DrawTexture(new Rect(list[i].rightChild.x,
                                                  list[i].rightChild.y, 2,
                                                  list[i].height),black);
                }
                
            }
            
            
            
        }
        if(Event.current.type.Equals(EventType.Repaint))
        {
            
            for(int i = TreeNode.counter/2; i < TreeNode.counter; i++)//only colors rooms
            {
                Graphics.DrawTexture(new Rect(list[i].roomX, list[i].roomY, list[i].roomWidth, list[i].roomHeight),green);
                //print(i + " " + list[i].roomX + ", " + list[i].roomY);
            }
            
            for(int j = 0; j < TreeNode.counter/2; j++)//only colors corridors
            {
                Graphics.DrawTexture(new Rect(list[j].roomX, list[j].roomY, list[j].roomWidth, list[j].roomHeight),red);
                //print(i + " " + list[i].roomX + ", " + list[i].roomY);
            }
        }
        
    }
   
}